package cn.yhjz.platform.system.service.impl;

import cn.yhjz.common.Pager;
import cn.yhjz.platform.system.entity.SysUserRole;
import cn.yhjz.platform.system.constant.Constant;
import cn.yhjz.platform.system.entity.SysUser;
import cn.yhjz.platform.system.mapper.SysMenuMapper;
import cn.yhjz.platform.system.mapper.SysUserMapper;
import cn.yhjz.platform.system.mapper.SysUserRoleMapper;
import cn.yhjz.platform.system.service.ISysUserService;
import cn.yhjz.platform.system.vo.SysMenuVo;
import cn.yhjz.platform.system.vo.SysUserVo;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class SysUserServiceImpl implements ISysUserService {

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public List<SysMenuVo> getUserMenuList(SysUser sysUser) {
        //如果是管理员，就返回所有
        if (Constant.ADMIN_USER_NAME.equals(sysUser.getUsername())) {
            return this.sysMenuMapper.getAllMenuListByRoleCodes();
        }
        //查询用户的角色
        QueryWrapper userRoleQueryWrapper = new QueryWrapper();
        SysUserRole paramUserRole = new SysUserRole();
        paramUserRole.setUserCode(sysUser.getUserCode());
        userRoleQueryWrapper.setEntity(paramUserRole);
        List<SysUserRole> sysUserRoleList = sysUserRoleMapper.selectList(userRoleQueryWrapper);
        //获得角色列表
        List<String> roleCodeList = new ArrayList<>();
        for (SysUserRole sysUserRole : sysUserRoleList) {
//            userBuilder.roles(sysUserRole.getRoleCode());
            roleCodeList.add(sysUserRole.getRoleCode());
        }
        if (CollUtil.isNotEmpty(roleCodeList)) {
            //获得菜单列表
            List<SysMenuVo> menuList = this.sysMenuMapper.getMenuListByRoleCodes(roleCodeList);
            return menuList;
        } else {
            return new ArrayList<>();
        }
    }

    @Override
    public Pager getPage(Long currentPage, Long pageSize, SysUser sysUser) {
        Pager pager = new Pager();
        Long count = sysUserMapper.getPageCount(sysUser);
        Long pageOffset = (currentPage - 1) * pageSize;
        List<SysUserVo> records = sysUserMapper.getPage(pageOffset, pageSize, sysUser);
        pager.setRows(records);
        pager.setCurrentPage(currentPage);
        pager.setPageSize(pageSize);
        pager.setTotalPageCount(count);
        return pager;
    }

    @Override
    public SysUser insert(SysUser sysUser) {
        int res = sysUserMapper.insert(sysUser);
        if (res > 0) {
            return sysUser;
        } else {
            return null;
        }
    }

    @Override
    public List<SysUser> select(SysUser sysUser) {
        return sysUserMapper.select(sysUser);
    }

    @Override
    public List<SysUserVo> selectVo(SysUser sysUser) {
        return sysUserMapper.selectVo(sysUser);
    }

    @Override
    public SysUser selectOne(SysUser sysUser) {
        return sysUserMapper.selectOne(sysUser);
    }

    @Override
    public SysUserVo selectOneVo(SysUser sysUser) {
        return sysUserMapper.selectOneVo(sysUser);
    }

    @Override
    public SysUser selectById(Long id) {
        return sysUserMapper.selectById(id);
    }

    @Override
    public int deleteById(Long id) {
        //先删除此用户的角色关联
        SysUser sysUser = sysUserMapper.selectById(id);
        sysUserRoleMapper.deleteByUserCode(sysUser.getUserCode());
        return sysUserMapper.deleteById(id);
    }

    @Override
    public int updateByIdWithNull(SysUser sysUser) {
        return sysUserMapper.updateByIdWithNull(sysUser);
    }

    @Override
    public int updateUserInfoById(SysUser sysUser) {
        return sysUserMapper.updateUserInfoById(sysUser);
    }

    @Override
    public int updateById(SysUser sysUser) {
        return sysUserMapper.updateById(sysUser);
    }

    @Override
    public int update(SysUser entity, SysUser param) {
        return sysUserMapper.update(entity, param);
    }

    @Override
    public int updateWithNull(SysUser entity, SysUser param) {
        return sysUserMapper.updateWithNull(entity, param);
    }

    @Override
    public int resetPassword(Long id, String password) {
        return sysUserMapper.resetPassword(id, password);
    }
}
